package sale.wxb.loveshopping.entity.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import sale.wxb.loveshopping.enums.ResponseEnum;

@Data
@ApiModel(description = "响应结果")
public class Result<T> {
    @ApiModelProperty("状态码")
    private int code;
    @ApiModelProperty("响应信息")
    private String message;
    @ApiModelProperty("数据")
    private T data;

    public Result(ResponseEnum code, String message, T data) {
        this.code = code.getCode();
        this.message = message;
        this.data = data;
    }

    public static Result<Boolean> send(boolean result, String message) {
        return result ? success(true, message) : fail(message);
    }

    public static <T> Result<T> send(boolean result) {
        return result ? success() : fail();
    }

    public static <T> Result<T> success() {
        return new Result<>(ResponseEnum.SUCCESS, ResponseEnum.SUCCESS.getMessage(), null);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(ResponseEnum.SUCCESS, ResponseEnum.SUCCESS.getMessage(), data);
    }

    public static <T> Result<T> success(T data, String message) {
        return new Result<>(ResponseEnum.SUCCESS, message, data);
    }

    public static <T> Result<T> fail() {
        return new Result<>(ResponseEnum.BUSINESS, ResponseEnum.SUCCESS.getMessage(), null);
    }

    public static <T> Result<T> fail(ResponseEnum code) {
        return new Result<>(code, code.getMessage(), null);
    }

    public static <T> Result<T> fail(String message) {
        return new Result<>(ResponseEnum.BUSINESS, message, null);
    }

    public static <T> Result<T> fail(ResponseEnum code, String message) {
        return new Result<>(code, message, null);
    }

    public static <T> Result<T> build(ResponseEnum code, T data, String message) {
        return new Result<>(code, message, data);
    }
}
